package com.yf.ability.face.factory;

import com.yf.ability.config.dto.CfgPropDTO;
import com.yf.ability.config.enums.ConfigType;
import com.yf.ability.config.enums.FuncSwitch;
import com.yf.ability.config.enums.ProviderType;
import com.yf.ability.config.service.CfgPropService;
import com.yf.ability.config.service.CfgSwitchService;
import com.yf.ability.face.service.ThirdFaceService;
import com.yf.ability.face.service.impl.EmptyFaceServiceImpl;
import com.yf.base.api.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FaceFactory {


    @Autowired
    private ThirdFaceService baiduFaceService;

    @Autowired
    private ThirdFaceService aliyunFaceService;

    @Autowired
    private ThirdFaceService tencentFaceService;

    @Autowired
    private EmptyFaceServiceImpl emptyFaceService;

    @Autowired
    private CfgPropService cfgPropService;

    @Autowired
    private CfgSwitchService cfgSwitchService;


    /**
     * 获取工厂实例
     * @return
     */
    public ThirdFaceService getService(){

        // 如果没空，执行一个空实现
        if(!cfgSwitchService.isOn(FuncSwitch.FACE_LOGIN)){
            return emptyFaceService;
        }

        // 从缓存中获取配置
        CfgPropDTO cfg = cfgPropService.detail(ConfigType.FACE, null);

        if(cfg == null){
            throw new ServiceException("找不到默认的人脸识别配置，请确认！");
        }

        if(ProviderType.BAIDU.equalsIgnoreCase(cfg.getProvider())){
            return baiduFaceService;
        }

        if(ProviderType.ALIYUN.equalsIgnoreCase(cfg.getProvider())){
            return aliyunFaceService;
        }

        if(ProviderType.QCLOUD.equalsIgnoreCase(cfg.getProvider())){
            return tencentFaceService;
        }

        // 没有正确实例返回空的
        return emptyFaceService;

    }
}
